const Mock = require('mockjs')

let list = []

const count = 50

for (let i = 0; i < count; i++) {
  list.push(Mock.mock({
    StudentID: '@increment',
    name: '@cname()',
    Studentnumber: '@integer(1,8)',
    sex: '@integer(0,1)',
    ClassID: '1',
    telphone: '12345678910',
    home: '@province()' + '@city()' + '@county()',
    IDnumber: '1234565789123456789',
    SchoolID: '1',
    status: 'true'
  }))
}

module.exports = [
  {
    url: '/student/list',
    type: 'get',
    response: config => {
      const { page, limit, title, type, sort } = config.query
      var mockList = list.filter(item => {
        if (type && item.type !== type) return false
        if (title && !item.name.includes(title)) return false
        return true
      })
      if (sort === '-id') {
        // eslint-disable-next-line no-const-assign
        mockList = mockList.reverse()
      }
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/student/create',
    type: 'post',
    response: config => {
      const data = Object.assign({}, config.body)
      list.push(data)
      return {
        code: 20000,
        data: {}
      }
    }
  },
  {
    url: '/student/updata',
    type: 'put',
    response: config => {
      const data = Object.assign({}, config.body)
      const indexOf = list.findIndex(item => {
        if (item.StudentID === data.StudentID) {
          return true
        }
      })
      list.splice(indexOf, 1, data)
      return {
        code: 2000
      }
    }
  },
  {
    url: '/student/remove',
    type: 'delete',
    response: config => {
      const { id } = Object.assign({}, config.query)
      list = list.filter(item => item.StudentID !== Number(id))
      return {
        code: 2000
      }
    }
  }
]
